package com.lafabrick.components
{
	import com.lafabrick.events.*;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.core.mx_internal;
	
	use namespace mx_internal;
	
	/**
	 * Composant étendu de ResizeWindow. 
	 * Ajout des fonctionnalitées Minimize.
	 */

	[IconFile("ExpandedWindowIcon.png")]
	
	/**
	 * Evénement déclenché lors du clic sur l'icône "MINIMIZE"
	 */
	[Event(name="minimize", type="flash.events.Event")]
	
	/**
	 * Evénement déclenché lors du clic sur l'icône "MAXIMIZE"
	 */
	[Event(name="maximize", type="flash.events.Event")]
	
	/**
	 * Définit la skin du bouton Minimize Down.
	 */
	[Style(name="minimizeButtonDownSkin", type="Class", inherit="no")]
	
	/**
	 * Définit la skin du bouton Minimize Over.
	 */
	[Style(name="minimizeButtonOverSkin", type="Class", inherit="no")]
	
	/**
	 * Définit la skin du bouton Minimize Up.
	 */
	[Style(name="minimizeButtonUpSkin", type="Class", inherit="no")]
	
	/**
	 * Définit la skin du bouton Maximize Down.
	 */
	[Style(name="maximizeButtonDownSkin", type="Class", inherit="no")]
	
	/**
	 * Définit la skin du bouton Maximize Over.
	 */
	[Style(name="maximizeButtonOverSkin", type="Class", inherit="no")]
	
	/**
	 * Définit la skin du bouton Maximize Up.
	 */
	[Style(name="maximizeButtonUpSkin", type="Class", inherit="no")]
	
	
	/**
	 * Composant étendu de ResizableWindow. 
	 * Ajout des fonctionnalitées MINIMIZE, MAXIMIZE (event).
	 */
	public class ExpandedWindow extends ResizableWindow
	{
		
		
		[Embed(source="../assets/button/minimizeDown.png")]
		private var MIN_BUTTON_DOWN:Class;
		
		[Embed(source="../assets/button/minimizeOver.png")]
		private var MIN_BUTTON_OVER:Class;
		
		[Embed(source="../assets/button/minimizeUp.png")]
		private var MIN_BUTTON_UP:Class;
		
		[Embed(source="../assets/button/maximizeDown.png")]
		private var MAX_BUTTON_DOWN:Class;
		
		[Embed(source="../assets/button/maximizeOver.png")]
		private var MAX_BUTTON_OVER:Class;
		
		[Embed(source="../assets/button/maximizeUp.png")]
		private var MAX_BUTTON_UP:Class;
		
		private var _minimizeEnabled : Boolean = false;
		
		private var _minimizeButton:Button = null;
		private var _maximizeButton:Button = null;
		
		private var _maxVisible:Boolean = false;
		private var _minVisible:Boolean = true;
		
		/**
		 * Constructeur
		 */ 
		public function ExpandedWindow() 
		{
			super();
		}
		
		[Inspectable(defaultValue=false, enumeration="true,false", verbose=1, category="Expanded Properties")]
		public function set minimizeEnabled(value:Boolean) : void {
			_minimizeEnabled = value;
		}
		/**
		 * Définit si le composant est minimizable. Le boutons MINIMIZE/MAXIMIZE sera apparent si minimizeEnabled = true.
		 * @param value:Boolean true / false
		 */
		public function get minimizeEnabled() : Boolean {
			return this._minimizeEnabled;
		}
		
		private var _onlyMinimize:Boolean = false;
		
		[Inspectable(defaultValue=false, enumeration="true,false", verbose=1, category="Expanded Properties")]
		public function set onlyMinimize(value:Boolean) : void {
			_onlyMinimize = value;
		}
		/**
		 * Définit MINIMIZE comme étant le seul état possible pour la fonctionnalitée minimizeEnabled = true.
		 * @param value:Boolean true / false
		 */
		public function get onlyMinimize() : Boolean {
			return this._onlyMinimize;
		}
		
		/**
		 * Inversion du prochain évènement<br/>
		 * L'apparence du bouton change en conséquence
		 */
		public function switchMinimizeMaximize() : void
		{
			if (onlyMinimize != true)
			{
				_maxVisible = !_maxVisible;
				_minVisible = !_minVisible;
			}
		}
		
		private var _isSwitchToMinimize:Boolean = false;
		private var _isSwitchToMaximize:Boolean = false;
		
		/**
		 * Définition du prochain évènement : au clic su le bouton minimize/maximize, "minimize" sera envoyé<br/>
		 * L'apparence du bouton change en conséquence
		 */
		public function switchToMinimize() : void
		{
			if (onlyMinimize != true)
			{
				_maxVisible = false;
				_minVisible = true;
			}
		}
		
		/**
		 * Définition du prochain évènement : au clic su le bouton minimize/maximize, "maximize" sera envoyé<br/>
		 * L'apparence du bouton change en conséquence
		 */
		public function switchToMaximize() : void
		{
			if (onlyMinimize != true)
			{
				_maxVisible = true;
				_minVisible = false;
			}
		}
		
		private function addButton():void
		{
			if(this._minimizeEnabled) {
				_minimizeButton = new Button();
				
				_minimizeButton.focusEnabled = false;
				_minimizeButton.styleName = this;
				
				_minimizeButton.setStyle("upSkin", MIN_BUTTON_UP);
				_minimizeButton.setStyle("overSkin", MIN_BUTTON_OVER);
				_minimizeButton.setStyle("downSkin", MIN_BUTTON_DOWN);
				_minimizeButton.addEventListener(MouseEvent.CLICK, minButtonClickHandler);
				
				
				titleBar.addChild(_minimizeButton);
				
				
				
				_maximizeButton = new Button();
				
				
				_maximizeButton.focusEnabled = false;
				_maximizeButton.styleName = this;
				
				_maximizeButton.setStyle("upSkin", MAX_BUTTON_UP);
				_maximizeButton.setStyle("overSkin", MAX_BUTTON_OVER);
				_maximizeButton.setStyle("downSkin", MAX_BUTTON_DOWN);
				_maximizeButton.addEventListener(MouseEvent.CLICK, maxButtonClickHandler);
				
				
				titleBar.addChild(_maximizeButton);
				
				
				
				_maximizeButton.visible = false;
				_minimizeButton.visible = true;
			}
			
			else {
				titleBar.removeChild(_minimizeButton);
				_minimizeButton = null;
				
				titleBar.removeChild(_maximizeButton);
				_maximizeButton = null;				
			}

		}
		
		private function layoutWindowButtons():void{
			
			var pxBox:Number = title != "" ? this.titleTextField.x+this.titleTextField.getExplicitOrMeasuredWidth() : 0;
			var wiBox:Number;
				
			if(_minimizeButton != null) {
				
				_maximizeButton.visible = _maxVisible;
				_minimizeButton.visible = _minVisible;
				
				_minimizeButton.setActualSize(
            		_minimizeButton.getExplicitOrMeasuredWidth(),
            		_minimizeButton.getExplicitOrMeasuredHeight());
			
				_maximizeButton.setActualSize(
            		_maximizeButton.getExplicitOrMeasuredWidth(),
            		_maximizeButton.getExplicitOrMeasuredHeight());	
            		
				if(showCloseButton) {	
                    wiBox = title != "" ? titleBar.width-_minimizeButton.width-closeButton.width-4*_buttonSpacing-pxBox : titleBar.width-_minimizeButton.width-closeButton.width-4*buttonSpacing;
							
                    _minimizeButton.move(titleBar.width - (_minimizeButton.width + closeButton.width + buttonSpacing)  - buttonMarginRight,
                    		buttonMarginTop);	
                    		
                    _maximizeButton.move(titleBar.width - (_maximizeButton.width + closeButton.width + buttonSpacing)  - buttonMarginRight,
                    		buttonMarginTop);
				}
				else {
					
					wiBox = title != "" ? titleBar.width-_minimizeButton.width-3*_buttonSpacing-pxBox : titleBar.width-_minimizeButton.width-3*_buttonSpacing;
					
					_minimizeButton.move(titleBar.width - (_minimizeButton.width + buttonSpacing)  - buttonMarginRight,
                    		buttonMarginTop);	
                    		
                    _maximizeButton.move(titleBar.width - (_maximizeButton.width + buttonSpacing)  - buttonMarginRight,
                    		buttonMarginTop);	
				}
					
			}

			else {	
				if(showCloseButton) {
					wiBox = title != "" ? titleBar.width-closeButton.width-pxBox-3*_buttonSpacing : titleBar.width-closeButton.width-3*_buttonSpacing;
				}
				else {
					wiBox = title != "" ? titleBar.width-pxBox-2*_buttonSpacing : titleBar.width-2*_buttonSpacing;
				}
			}	
			
			
			hBox.move(pxBox+this._buttonSpacing, this._buttonMarginTop);
			hBox.setActualSize(wiBox, titleBar.height-this._buttonMarginTop);
			
			
		}
		
		private function minButtonClickHandler(event:MouseEvent):void
		{
			this.switchToMaximize();
			dispatchEvent(new Event("minimize"));
				
			_minimizeButton.visible = this._minVisible;
			_maximizeButton.visible = this._maxVisible;
			
		}
		
		private function maxButtonClickHandler(event:MouseEvent):void
		{
			this.switchToMinimize();
			dispatchEvent(new Event("maximize"));

			_minimizeButton.visible = this._minVisible;
			_maximizeButton.visible = this._maxVisible;
			
		}
		
		
		// GESTION STYLE 
		
		/**
		 * @private
		 */
		override protected function createChildren():void {
            super.createChildren();
            
            if (this._minimizeEnabled) {
            	
				if (getStyle("minimizeButtonDownSkin"))
					MIN_BUTTON_DOWN = getStyle("minimizeButtonDownSkin");
				
				if (getStyle("minimizeButtonOverSkin"))
					MIN_BUTTON_OVER = getStyle("minimizeButtonOverSkin");
				
				if (getStyle("minimizeButtonUpSkin"))
					MIN_BUTTON_UP = getStyle("minimizeButtonUpSkin");
				
				if (getStyle("maximizeButtonDownSkin"))
					MAX_BUTTON_DOWN = getStyle("maximizeButtonDownSkin");
				
				if (getStyle("maximizeButtonOverSkin"))
					MAX_BUTTON_OVER = getStyle("maximizeButtonOverSkin");
				
				if (getStyle("maximizeButtonUpSkin"))
					MAX_BUTTON_UP = getStyle("maximizeButtonUpSkin");
            
            	addButton();
            }
            
          //  trace("_minimizeButton : "+_minimizeButton);
            
            
  		}
  		
		/**
		 * @private
		 */
    	override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if (this._minimizeEnabled) {
            
           	
				if (getStyle("minimizeButtonDownSkin")) {
					MIN_BUTTON_DOWN = getStyle("minimizeButtonDownSkin");
					_minimizeButton.setStyle("downSkin", MIN_BUTTON_DOWN);
				}
				
				if (getStyle("minimizeButtonOverSkin")) {
					MIN_BUTTON_OVER = getStyle("minimizeButtonOverSkin");
					_minimizeButton.setStyle("overSkin", MIN_BUTTON_OVER);
				}
				
				if (getStyle("minimizeButtonUpSkin")) {
					MIN_BUTTON_UP = getStyle("minimizeButtonUpSkin");
					_minimizeButton.setStyle("upSkin", MIN_BUTTON_UP);
				}
				
				if (getStyle("maximizeButtonDownSkin")) {
					MAX_BUTTON_DOWN = getStyle("maximizeButtonDownSkin");
					_maximizeButton.setStyle("downSkin", MAX_BUTTON_DOWN);
				}
				
				if (getStyle("maximizeButtonOverSkin")) {
					MAX_BUTTON_OVER = getStyle("maximizeButtonOverSkin");
					_maximizeButton.setStyle("overSkin", MAX_BUTTON_OVER);
				}
				
				if (getStyle("maximizeButtonUpSkin")) {
					MAX_BUTTON_UP = getStyle("maximizeButtonUpSkin");
					_maximizeButton.setStyle("upSkin", MAX_BUTTON_UP);
				}
				
			}
			
			layoutWindowButtons();
					
		}
	}
}